package com.platform.user.controller;

import com.platform.bean.PageInfo;
import com.platform.bean.Result;
import com.platform.bean.SimilarImageResult;
import com.platform.user.entity.LostHelpNotice;
import com.platform.user.service.ItemAuditService;
import com.platform.user.service.LostHelpNoticeService;
import com.platform.utils.BaiduSimilarImageServerUtils;
import org.json.JSONException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;

@RestController
@RequestMapping("/lostHelpNotice")
public class LostHelpNoticeController {

    @Resource
    private LostHelpNoticeService lostHelpNoticeService;
    @Resource
    private ItemAuditService itemAuditService;

    @PostMapping("searchSimilarImage")
    public Result<Map<String, Object>> searchSimilarImage(@RequestBody Map<String, Object> params) throws JSONException, IOException {
        Map<String, Object> imageSimilarResp = BaiduSimilarImageServerUtils.searchSimilarImageBrief(params);
        Map<String, Object> resp = new HashMap<>();
        List<SimilarImageResult> similarImageResultList = (List<SimilarImageResult>) imageSimilarResp.get("similarImageResultList");
        List<LostHelpNotice> lostHelpNoticeList = new ArrayList<>();
        for (SimilarImageResult x: similarImageResultList) {
            lostHelpNoticeList.add(lostHelpNoticeService.queryOneLostHelpNoticeByItemId(Integer.valueOf(x.getBrief())));
        }
        resp.put("lostHelpNoticeList", lostHelpNoticeList);
        return Result.ok(resp);
    }


    @PostMapping("redisplayLostHelpNotice")
    public Result<Map<String, Object>> redisplayLostHelpNotice(@RequestBody Map<String, Object> params) throws JSONException, IOException {
        lostHelpNoticeService.redisplayLostHelpNotice(params);
        return Result.ok("ok");
    }

    @PostMapping("illegalWithdrawalLostHelpNotice")
    public Result<Map<String, Object>> illegalWithdrawalLostHelpNotice(@RequestBody Map<String, Object> params) throws JSONException, IOException {
        lostHelpNoticeService.illegalWithdrawalLostHelpNotice(params);
        return Result.ok("ok");
    }

    @PostMapping("setItemHadFound")
    public Result<Map<String, Object>> setItemHadFound(@RequestBody Map<String, Object> params) throws JSONException, IOException {
        lostHelpNoticeService.setItemHadFound(params);
        return Result.ok("ok");
    }

    @PostMapping("queryOneLostHelpNoticeByItemId")
    public Result<Map<String, Object>> queryOneLostHelpNoticeByItemId(@RequestBody Map<String, Object> params) {
        LostHelpNotice lostHelpNotice = lostHelpNoticeService.queryOneLostHelpNoticeByItemId(params);
        return Result.ok(lostHelpNotice);
    }

    @PostMapping("queryLostHelpNoticeByPage")
    public Result<Map<String, Object>> queryLostHelpNoticeByPage(@RequestBody Map<String, Object> params) {
        PageInfo<LostHelpNotice> lostHelpNoticePageInfo = lostHelpNoticeService.queryLostHelpNoticeByPage(params);
        return Result.ok(lostHelpNoticePageInfo);
    }

    @PostMapping("addLostHelpNotice")
    public Result<Map<String, Object>> addLostHelpNotice(@RequestBody Map<String, Object> params) throws ParseException {
        LostHelpNotice lostHelpNotice = lostHelpNoticeService.addLostHelpNotice(params);
        Integer id = lostHelpNotice.getId();
        if (id != null) {
            params.put("itemId", id);
            params.put("itemSort", 0);
            params.put("itemName", lostHelpNotice.getItemName());
            itemAuditService.addItemAudit(params);
        }
        return Result.ok("申请成功");
    }
}
